
package gui;

import java.awt.Frame;
import javax.swing.DefaultComboBoxModel;
import output.FileSpectrumOutputModule;
import output.OscOutputModule;

/**
 * Dialog for the settings of the Beat Analyser.
 *
 * @author  Stefan Marks
 * @version 1.0 - 15.06.2013: Created
 * @version 1.1 - 20.06.2013: Adapted to selectable colour maps and OSC output
 */
public class PreferencesDialog extends javax.swing.JDialog
{

    public static enum UserChoice
    {
        CLOSE, CANCEL, ACCEPT;
    }

    /**
     * Creates a new preferences dialog.
     *
     * @param parent   the parent frame
     * @param settings the settings to represent
     */
    public PreferencesDialog(Frame parent)
    {
        super(parent, true);

        cbxModelColourMap = new DefaultComboBoxModel<ColourMap>(new ColourMap[] {
            RainbowColourMap.INSTANCE,
            FrequencyRainbowColourMap.INSTANCE});
        initComponents();

        pack();
        setLocationRelativeTo(parent);
    }

    public UserChoice showDialog()
    {
        userChoice = UserChoice.CLOSE;
        setVisible(true);
        return userChoice;
    }

    public void loadFileOutputSettings(FileSpectrumOutputModule output)
    {
        chkEnableLogfileOutput.setSelected(output.isEnabled());
        txtOutputFilename.setText(output.getOutputFilename());
    }

    public void applyFileOutputSettings(FileSpectrumOutputModule output)
    {
        output.setEnabled(chkEnableLogfileOutput.isSelected());
        output.setOutputFilename(txtOutputFilename.getText());
    }

    public void loadOscOutputSettings(OscOutputModule output)
    {
        chkEnableOscOutput.setSelected(output.isEnabled());
        txtNetworkAddress.setText(output.getTargetAddress());
        txtOscAddress.setText(output.getOscTargetAddress());
    }

    public void applyOscOutputSettings(OscOutputModule output)
    {
        output.setEnabled(chkEnableOscOutput.isSelected());
        output.setTargetAddress(txtNetworkAddress.getText());
        output.setOscTargetAddress(txtOscAddress.getText());
    }

    public ColourMap getColourMap()
    {
        return (ColourMap) cbxColourMap.getSelectedItem();
    }

    public void setColourMap(ColourMap colourMap)
    {
        cbxColourMap.setSelectedItem(colourMap);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {
        java.awt.GridBagConstraints gridBagConstraints;

        panelTabs = new javax.swing.JTabbedPane();
        pnlFileOutput = new javax.swing.JPanel();
        javax.swing.JLabel lblEnableFileOutput = new javax.swing.JLabel();
        chkEnableLogfileOutput = new javax.swing.JCheckBox();
        javax.swing.JLabel lblOutputFilename = new javax.swing.JLabel();
        txtOutputFilename = new javax.swing.JTextField();
        pnlOscOutput = new javax.swing.JPanel();
        javax.swing.JLabel lblEnableOscOutput = new javax.swing.JLabel();
        chkEnableOscOutput = new javax.swing.JCheckBox();
        javax.swing.JLabel lblNetworkAddress = new javax.swing.JLabel();
        txtNetworkAddress = new javax.swing.JTextField();
        javax.swing.JLabel lblOscAddress = new javax.swing.JLabel();
        txtOscAddress = new javax.swing.JTextField();
        pnlColourMap = new javax.swing.JPanel();
        javax.swing.JLabel lblColourMap = new javax.swing.JLabel();
        cbxColourMap = new javax.swing.JComboBox();
        javax.swing.JPanel pnlButtons = new javax.swing.JPanel();
        javax.swing.JButton btnCancel = new javax.swing.JButton();
        btnAccept = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Preferences");

        panelTabs.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        panelTabs.setPreferredSize(new java.awt.Dimension(400, 300));

        pnlFileOutput.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        pnlFileOutput.setLayout(new java.awt.GridBagLayout());

        lblEnableFileOutput.setText("Enable Logfile Output:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 6);
        pnlFileOutput.add(lblEnableFileOutput, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        pnlFileOutput.add(chkEnableLogfileOutput, gridBagConstraints);

        lblOutputFilename.setText("Filename:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 6);
        pnlFileOutput.add(lblOutputFilename, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 0);
        pnlFileOutput.add(txtOutputFilename, gridBagConstraints);

        panelTabs.addTab("File Output", pnlFileOutput);

        pnlOscOutput.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        pnlOscOutput.setLayout(new java.awt.GridBagLayout());

        lblEnableOscOutput.setText("Enable OSC Output:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 6);
        pnlOscOutput.add(lblEnableOscOutput, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 1.0;
        pnlOscOutput.add(chkEnableOscOutput, gridBagConstraints);

        lblNetworkAddress.setText("Target Address/Port:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 6);
        pnlOscOutput.add(lblNetworkAddress, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 0);
        pnlOscOutput.add(txtNetworkAddress, gridBagConstraints);

        lblOscAddress.setText("OSC Target Address:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 6);
        pnlOscOutput.add(lblOscAddress, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(7, 0, 0, 0);
        pnlOscOutput.add(txtOscAddress, gridBagConstraints);

        panelTabs.addTab("OSC Output", pnlOscOutput);

        pnlColourMap.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        pnlColourMap.setLayout(new java.awt.GridBagLayout());

        lblColourMap.setText("Colour Map:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 6);
        pnlColourMap.add(lblColourMap, gridBagConstraints);

        cbxColourMap.setModel(cbxModelColourMap);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        pnlColourMap.add(cbxColourMap, gridBagConstraints);

        panelTabs.addTab("Colour Map", pnlColourMap);

        getContentPane().add(panelTabs, java.awt.BorderLayout.CENTER);

        pnlButtons.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.TRAILING));

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                btnCancelActionPerformed(evt);
            }
        });
        pnlButtons.add(btnCancel);

        btnAccept.setText("Accept");
        btnAccept.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                btnAcceptActionPerformed(evt);
            }
        });
        pnlButtons.add(btnAccept);

        getContentPane().add(pnlButtons, java.awt.BorderLayout.PAGE_END);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnCancelActionPerformed
    {//GEN-HEADEREND:event_btnCancelActionPerformed
        userChoice = UserChoice.CANCEL;
        setVisible(false);
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnAcceptActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnAcceptActionPerformed
    {//GEN-HEADEREND:event_btnAcceptActionPerformed
        userChoice = UserChoice.ACCEPT;
        setVisible(false);
    }//GEN-LAST:event_btnAcceptActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAccept;
    private javax.swing.JComboBox cbxColourMap;
    private javax.swing.JCheckBox chkEnableLogfileOutput;
    private javax.swing.JCheckBox chkEnableOscOutput;
    private javax.swing.JTabbedPane panelTabs;
    private javax.swing.JPanel pnlColourMap;
    private javax.swing.JPanel pnlFileOutput;
    private javax.swing.JPanel pnlOscOutput;
    private javax.swing.JTextField txtNetworkAddress;
    private javax.swing.JTextField txtOscAddress;
    private javax.swing.JTextField txtOutputFilename;
    // End of variables declaration//GEN-END:variables

    private UserChoice userChoice;
    private DefaultComboBoxModel<ColourMap> cbxModelColourMap;
}
